local skill = fk.CreateSkill {
  name = "lb_zero__quanyi_duel_skill",
}

Fk:loadTranslationTable {
  ["lb_zero__quanyi_duel_skill"] = "决斗",
}

skill:addEffect("cardskill", {
  prompt = "#duel_skill",
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select ~= player
  end,
  target_filter = Util.CardTargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = effect.to
    local from = effect.from
    local responsers = { to, from }
    local currentTurn = 1
    local currentResponser = to

    effect.extra_data = effect.extra_data or {}
    local use_list = effect.extra_data.lb_zero__quanyi_duel or {} --这个打出改为使用感觉用的不少，下次再独立出来

    while currentResponser:isAlive() do
      local loopTimes = effect:getResponseTimes(currentResponser)

      local respond
      for i = 1, loopTimes do
        if table.contains(use_list, currentResponser.id) then -- 包含的角色会将打出改为使用
          local params = { ---@type AskToUseCardParams
            skill_name = 'slash',
            pattern = 'slash',
            cancelable = true,
            event_data = effect,
            extra_data = {must_targets = {currentResponser == to and from.id or to.id}, bypass_distances = true, bypass_times = true}
          }
          if loopTimes > 1 then
            params.prompt = "#AskForUseMultiCard:::slash:" .. i .. ":" .. loopTimes
          end
          respond = room:askToUseCard(currentResponser, params)  --这里是use了
          if respond then
            respond.extraUse = true
            room:useCard(respond)
          else
            break
          end
        else
          local params = { ---@type AskToUseCardParams
            skill_name = 'slash',
            pattern = 'slash',
            cancelable = true,
            event_data = effect
          }
          if loopTimes > 1 then
            params.prompt = "#AskForResponseMultiCard:::slash:" .. i .. ":" .. loopTimes
          end
          respond = room:askToResponse(currentResponser, params)
          if respond then
            room:responseCard(respond)
          else
            break
          end
        end
      end

      if not respond then
        break
      end

      currentTurn = currentTurn % 2 + 1
      currentResponser = responsers[currentTurn]
    end

    if currentResponser:isAlive() then
      room:damage({
        from = responsers[currentTurn % 2 + 1],
        to = currentResponser,
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = skill.name,
      })
    end
  end,
})

skill:addAI({
  on_effect = function(self, logic, effect)
    local from, to = effect.from, effect.to
    if #table.filter(from:getHandlyIds(), function(id)
          return Fk:getCardById(id).trueName == "slash" and not from:prohibitResponse(Fk:getCardById(id))
        end) < #table.filter(to:getHandlyIds(), function(id)
          return Fk:getCardById(id).trueName == "slash" and not to:prohibitResponse(Fk:getCardById(id))
        end) then
      from, to = to, from
    end
    logic:damage({
      from = from,
      to = to,
      card = effect.card,
      damage = 1,
      damageType = fk.NormalDamage,
      skillName = skill.name
    })
  end,
}, "__card_skill")

return skill
